package com.itunion.ddd.common.base.utils;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class HttpUtils {

	public static String getClientIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if (org.springframework.util.StringUtils.hasText(ip) && !"unKnown".equalsIgnoreCase(ip)) {
			int index = ip.indexOf(',');
			if (index != -1) {
				log.info("unKnown1111===={}"+ip);
				return ip.substring(0, index);
			} else {
				log.info("unKnown2222===={}"+ip);
				return ip;
			}
		}
		ip = request.getHeader("X-Real-IP");
		if (org.springframework.util.StringUtils.hasText(ip) && !"unKnown".equalsIgnoreCase(ip)) {
			log.info("X-Real-IP===={}"+ip);
			return ip;
		}
		log.info("RemoteAddr===={}"+request.getRemoteAddr());
		return request.getRemoteAddr();
	}
}
